import { ClassicModel } from "../../models/classic";
import {LikeModel} from "../../models/like"
let classicModel = new ClassicModel()
let likeModel = new LikeModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classic:'',
    latest:true,
    first:false,
    likeCount:0,
    likeStatus:false
  },
  onLoad: function (options) {
    classicModel.getLatest((res) => {
      this.setData({
          classic: res,
          likeCount: res.fav_nums,
          likeStatus: res.like_status
        })
    })
  },
  onLike:function(event){
    let like_or_cancel = event.detail.behavior
    likeModel.like(like_or_cancel, this.data.classic.id, this.data.classic.type)
  },
  onNext(e){
    this._onClassic('next')
  },
  onPrevious(e){
    this._onClassic('previous')
  },
  _onClassic(nextOrPrevious){
    let index = this.data.classic.index
    classicModel.getClassic(index, nextOrPrevious, (res)=>{
      this._getLikeStatus(res.id, res.type)
      this.setData({
        classic: res,
        latest: classicModel.isLatest(res.index),
        first:classicModel.isFirst(res.index)
      })
    })
  },
  _getLikeStatus(art_id, category){
    likeModel.getClassicLikeStatus(art_id, category, (res)=>{
      this.setData({
        likeStatus:res.like_status,
        likeCount: res.fav_nums
      })
    })
  },

  onShareAppMessage: function () {

  }
})